package orphaned;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.playcabbage.beans.CheckinBean2;
import net.playcabbage.database.operations.MapOps;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GridServlet extends HttpServlet {





	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


		
			try {
				req.setAttribute("pixelarray", makeSVGArray(0));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new ServletException();
				//e.printStackTrace();
			}
		

		RequestDispatcher requestDispatcher =  req.getRequestDispatcher("/templates/checkinvis2.vm");
		requestDispatcher.forward(req, resp);

	}

	private List<String> makeSVGArray(int level) throws SQLException {

		int width = 153;
		int height = 88;

		int circleSize = 3;
		int strokeWidth = 1;

		int xCoord = circleSize + strokeWidth;
		int yCoord = circleSize + strokeWidth;

		int advanceincrement = (circleSize + strokeWidth)*2;

		MapOps mo = new MapOps();
		String[] pList = mo.getArrayColorPixels(0);
		List<String> svgStrings = new ArrayList<String>();

		String hoverClass = "circleHover";
		
		int totalCount = 0;
		int innerCount = 0;
		int count = 0;

		String unmappedColor = "Black";

		while ( count < height ) {

			svgStrings.add("<text x=\"" + xCoord + "\" y=\"" + yCoord + "\" font-size=\"" + circleSize + "px\" fill=\"red\">" + count + "</text>");
			xCoord += advanceincrement;
			innerCount = 0;

			while (innerCount < width ) {

				if ( count < 1) {
					svgStrings.add("<text x=\"" + xCoord + "\" y=\"" + yCoord + "\" font-size=\"" + circleSize + "px\" fill=\"red\">" + (innerCount+1) + "</text>");
					xCoord += advanceincrement;
					innerCount ++;
				} else {
					if (pList[totalCount] == null) {
						svgStrings.add("<circle cx=\"" + xCoord + "\"cy=\"" + yCoord + "\"r=\"" + circleSize + "\" stroke=\"black\" stroke-width=\"" + strokeWidth + "\" fill=\"" + unmappedColor + "\" class=\"" + hoverClass + "\"" + "/> <p class=\"gridhiddeninfo\">This is hidden</p>");
						xCoord += advanceincrement;
						innerCount ++;
						totalCount ++; 
					} else {
						svgStrings.add("<circle cx=\"" + xCoord + "\"cy=\"" + yCoord + "\"r=\"" + circleSize + "\" stroke=\"black\" stroke-width=\"" + strokeWidth + "\" fill=\"" + pList[totalCount] + "\" class=\"" + hoverClass + "\"" + "/> <p class=\"gridhiddeninfo\">This is hidden</p>");
						xCoord += advanceincrement;
						innerCount ++;
						totalCount ++; 
					}
				}

			}

			xCoord = circleSize + strokeWidth;
			yCoord += advanceincrement;
			count ++;

		}
		return svgStrings;
	}




}
